package pl.cba.solgar.game.rogal.commands;

import pl.cba.solgar.game.rogal.CommandRecognizer;
import pl.cba.solgar.game.rogal.MessageConsole;
import pl.cba.solgar.game.rogal.Rogal;
import pl.cba.solgar.game.rogal.utils.BinarySearchTree;

public class QuitCommand extends RogalCmd {
	private BinarySearchTree tree;
	private CommandRecognizer rec;


	public QuitCommand(String keys, Rogal rogal, CommandRecognizer rec) {
		super(keys, rogal);
		this.rec = rec;
		tree = new BinarySearchTree();
		rec.addCommand(new Confirm("y", rogal), tree);
		rec.addCommand(new Reject("\n", rogal), tree);
		rec.addCommand(new Reject("n", rogal), tree);
	}

	public boolean execute() {
		MessageConsole.getInstance().postMessage("Do you really want to quit? [y/N].");
		rec.setCurrentTree(tree);
		rec.setCurrentDefaultCommand(new Reject("n", rogal));
		return false;
	}

	public String getShortDescription() {
		return "immiediatly quits the game";
	}

	private class Confirm extends RogalCmd {

		public Confirm(String keys, Rogal rogal) {
			super(keys, rogal);
		}

		public boolean execute() {
			Rogal.app.exit();
			return true;
		}

	}

	private class Reject extends RogalCmd {

		public Reject(String keys, Rogal rogal) {
			super(keys, rogal);
		}

		public boolean execute() {
			MessageConsole.getInstance().postMessage("Good! Carry on then.");
			return true;
		}

	}

}
